package com.yz.group5.med_booking.controller;

import com.yz.group5.med_booking.entity.MedicalRecord;
import com.yz.group5.med_booking.service.MedicalRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin(origins = "http://127.0.0.1:5500") // 允许前端地址跨域访问
@RestController // 标记当前类是一个控制器类，返回结果为JSON数据结构
@RequestMapping("/medicalRecord") // 统一前缀，前端访问 /medicalRecord开头的接口
public class MedicalRecordController {

    @Autowired // 注入Service，从容器中拿medicalRecordService实现类对象
    private MedicalRecordService medicalRecordService;

    /**
     * 获取所有诊疗记录
     */
    @GetMapping
    public List<MedicalRecord> medicalRecordList() {
        return medicalRecordService.list();
    }

    /**
     * 根据挂号ID获取诊疗记录
     */
    @GetMapping("/byRegistration/{registrationId}")
    public List<MedicalRecord> getByRegistrationId(@PathVariable Integer registrationId) {
        return medicalRecordService.getByRegistrationId(registrationId);
    }

    /**
     * 添加诊疗记录
     */
    @PostMapping("/add")
    // 修改返回值类型为 MedicalRecord
    public boolean add(@RequestBody MedicalRecord medicalRecord) {
        return medicalRecordService.save(medicalRecord);
    }
    /**
     * 更新诊疗记录
     */
    @PostMapping("/update")
    public boolean update(@RequestBody MedicalRecord medicalRecord) {
        return medicalRecordService.updateById(medicalRecord);
    }

    /**
     * 提交住院申请
     */
    @PostMapping("/applyHospitalization/{id}")
    public boolean applyHospitalization(@PathVariable Integer id) {
        return medicalRecordService.applyHospitalization(id);
    }

    /**
     * 删除诊疗记录
     */
    @DeleteMapping("/{id}")
    public boolean deleteMedicalRecord(@PathVariable Integer id) {
        return medicalRecordService.removeById(id);
    }
}